package com.ruoyi.framework.config;


import com.ruoyi.framework.listener.MyFileListener;
import com.ruoyi.system.domain.Listen;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Author:let_free
 * @Date:2024/6/6/14:22
 **/
@Component
public class FileMonitorConfig {
    @Autowired
    private MyFileListener myFileListener;

    @Autowired
    private Listen listen;

    @Bean
    public FileAlterationMonitor fileAlterationMonitor() {
        long pollingInterval = 500;
//        File directory = new File(listen.getPath()); // 监听的目录
//        设置监听目录并且过滤文件 只监听以.bin.bz2结尾文件
        FileAlterationObserver observer = new FileAlterationObserver(listen.getPath(),
                FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter(".bin.bz2")));
        observer.addListener(myFileListener);
        FileAlterationMonitor monitor = new FileAlterationMonitor(pollingInterval, observer);
        return monitor;
    }
}
